package com.hg.admin.db.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdiy.core.Args;
import org.jdiy.core.Ls;
import org.jdiy.core.Rs;


import com.hg.admin.db.dto.UserAddDTO;
import com.hg.admin.db.dto.UserDTO;
import com.hg.admin.db.dto.UserOperationDTO;
import com.hg.util.common.Common;
import com.hg.util.db.IDao;

public class UserDAO extends IDao
{
	String table = db_prefix+"user";
	String table_view = db_prefix+"user_view";
	String table_money_log = db_prefix+"user_money_log";
	String table_fx_money_log = db_prefix+"fx_user_money_log";
	String table_score_log = db_prefix+"user_score_log";
	public UserDTO encapsulation(Rs rs){
		UserDTO dto = new UserDTO();
		
		dto.setId(rs.getInt("id"));
		dto.setUser_name(rs.getString("user_name"));
		dto.setMobile(rs.getString("mobile"));
		dto.setMoney(rs.getDouble("money"));
		dto.setScore(rs.getDouble("score"));
		dto.setFx_money(rs.getDouble("fx_money"));
		dto.setGroup_id(rs.getInt("group_id"));
		dto.setGroup_name(rs.getString("group_name"));
		dto.setCreate_time(rs.getInt("create_time"));
		dto.setIs_effect(rs.getInt("is_effect"));
		
		
		return dto;
	}
	
	/**
	 * @param sql
	 * @return	total
	 * 			rows :UserDTO集合
	 */
	public Map<String, Object> index(Map<String, Object> args_data){
		Map<String, Object> map = new HashMap<String, Object>();
		Args args = new Args(table_view);
		args.setAbsPage((int)args_data.get("absPage"));
		args.setPageSize((int)args_data.get("pageSize"));
		args.setFilter((String)args_data.get("filter"));
		Ls ls = dao.ls(args);
		map.put("total", ls.getRowCount());
		List<UserDTO> list = new ArrayList<UserDTO>();
		for(Rs rs:ls.getItems()){
			list.add(this.encapsulation(rs));
		}
		map.put("rows", list);
		return map;
	}
	
	public boolean creatUser(UserAddDTO dto) {
		Rs rs = null;
		try {
			rs = new Rs(table);
			rs.set("user_name", dto.getUser_name())
					.set("user_pwd", dto.getUser_pwd())
					.set("mobile", dto.getMobile())
					.set("create_time", dto.getCreate_time())
					.set("update_time", dto.getCreate_time())
					.set("is_effect", dto.getIs_effect())
					.set("group_id", dto.getGroup_id());

			dao.save(rs);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		} catch (Error e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean updateUser(UserAddDTO dto){
		try{
			String sql = "update "+table+" set user_name='"+dto.getUser_name()+"',mobile='"+dto.getMobile()
					+"',is_effect="+dto.getIs_effect()+",group_id="+dto.getGroup_id();
			
			if(null==dto.getUser_pwd() || "".equals(dto.getUser_pwd())){
				
			}else{
				sql=sql+",user_pwd='"+dto.getUser_pwd()+"'";
			}
			sql = sql+" where id="+dto.getId();
			dao.exec(sql);
			return true;
		}catch(Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public UserAddDTO getOne(int id){
		UserAddDTO dto = new UserAddDTO();
		Args args = new Args(table);
		args.setFilter("id="+id);
		Rs rs = dao.rs(args);
		if(rs.isNull()){
			return null;
		}else{
			dto.setId(rs.getInt("id"));
			dto.setUser_name(rs.getString("user_name"));
			dto.setMobile(rs.getString("mobile"));
			dto.setGroup_id(rs.getInt("group_id"));
			dto.setIs_effect(rs.getInt("is_effect"));
		}
		return dto;
	}
	
	public boolean del(int id){
		try{
			dao.exec("delete from "+table+" where id="+id);
			return true;
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return false;
	}
	
	public boolean dels(String ids){
		try{
			dao.exec("delete from "+table+" where id in ("+ids+")");
			return true;
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return false;
	}
	
	public boolean updataAccount(UserOperationDTO dto){
		try{
			double money;
			double fx_money;
			double score;
			dao.transBegin();
			//获取用户余额、积分、佣金
			Args args = new Args(table);
			args.setFilter("id="+dto.getU_id());
			Rs rs1 = dao.rs(args);
			if(rs1.isNull()){
				return false;
			}else{				
				//dto.setUser_name(rs1.getString("user_name"));
				money=rs1.getDecimal("money").doubleValue()+dto.getMoney();
				fx_money=rs1.getDecimal("fx_money").doubleValue()+dto.getFx_money();
				score=rs1.getDecimal("score").doubleValue()+dto.getScore();
			}
			//更新用户表
			dao.exec("update "+table+" set money= "+money+",fx_money= "+
			fx_money+",score= "+score+" where id ="+dto.getU_id());
			//新增用户账户明细
			insertMoney_log(dto);
			//新增用户佣金明细
			insertFx_money_log(dto);
			//新增用户积分明细
			insertScore_log(dto);
			dao.transCommit();
			return true;
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return false;
	}
	
	private void insertMoney_log(UserOperationDTO dto){
		
		if(dto.getMoney()!=0){
		Rs rs = new Rs(table_money_log);
		rs.set("log_info", dto.getMoneyDesc());
		rs.set("log_time", Common.getTimeStamp());
		rs.set("log_admin_id", dto.getAdmin_id());
		rs.set("money",dto.getMoney());
		rs.set("user_id", dto.getU_id());
		rs.set("type",3);
		dao.save(rs);
		}
		
	}
	
	private void insertFx_money_log(UserOperationDTO dto){
		
		if(dto.getFx_money()!=0){
		Rs rs = new Rs(table_fx_money_log);
		rs.set("log", dto.getFx_moneyDesc());
		rs.set("create_time", Common.getTimeStamp());
		rs.set("log_admin_id", dto.getAdmin_id());
		rs.set("money",dto.getFx_money());
		rs.set("user_id", dto.getU_id());
		rs.set("type",0);
		dao.save(rs);
		}
	}
	private void insertScore_log(UserOperationDTO dto){
		
		if(dto.getScore()!=0){
		Rs rs = new Rs(table_score_log);
		rs.set("log_info",dto.getScoreDesc());
		rs.set("log_time", Common.getTimeStamp());
		rs.set("log_admin_id", dto.getAdmin_id());
		rs.set("score",dto.getScore());
		rs.set("user_id", dto.getU_id());
		rs.set("type",3);
		dao.save(rs);
		}
	}
}
